<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Login Controller
 *
 * @package     VisualQ Local System
 * @author      CSU OJT Students '10 - '11
 * @copyright   Copyright (c) 2010 - 2011, Bitstop Network Services, Inc.
 * @version     0.1b January 01, 2011
 */

class Login extends Controller {

    function Login()
    {
        parent::Controller();

        // Check if the necessary settings are set
        $this->load->model('settings_model');
        if( ! ($this->settings_model->check_settings() &&
                $this->settings_model->check_services() &&
                $this->settings_model->check_accounts() &&
                $this->settings_model->check_accounts_services()))
        {
            redirect(base_url());
        }

        $this->lang->load('local', 'English');
        $this->load->helper('cookie');

        // Check if user is logged in
        if($this->session->userdata('id'))
        {
            if(strcmp($this->session->userdata('account_type'), 'agent') == 0)
            {
                redirect(base_url() . 'agent/');
            }
            if(strcmp($this->session->userdata('account_type'), 'manager') == 0)
            {
                redirect(base_url() . 'manager/');
            }
        }

        // Check if the unit is registered
        if($this->session->userdata('unit_name'))
        {
            $type = $this->session->userdata('unit_type');
            if(strcmp($type, 'Front Desk') == 0 || strcmp($type, 'Display') == 0)
            {
                // Check if the unit front desk or the display cookie is active
                if(get_cookie('unit'))
                {
                    redirect(base_url() . 'unit/');
                }
            }
        }

        //$this->output->enable_profiler(TRUE);
    }

    function index()
    {
        // Get settings
        $query = $this->db->get('settings');
        if($query->num_rows > 0)
        {
            foreach($query->result_array() as $row)
            {
                $data['settings'][$row['name']] = $row['value'];
            }
        }

        // Check if the computer is registered
        if($this->session->userdata('unit_name'))
        {
            $data['header'] = $this->session->userdata('unit_name');
            $data['type'] = $this->session->userdata('unit_type');
        }
        else
        {
            $data['header'] = $this->lang->line('login_unregister');
            $data['type'] = '';
        }

        // Get the server IP
        if(strcmp($data['settings']['logo'], '') == 0)
        {
            $data['settings']['logo'] = 'png';
        }
        $ip = gethostbynamel('');
        $data['ip'] = $ip[0];

        $this->load->view('login_view', $data);
    }

    function authenticate()
    {
        $this->load->model(array('account_model', 'statistics_model'));

        $params = array(
            'un' => $this->input->post('user') ? $this->input->post('user') : NULL,
            'pw' => $this->input->post('pword') ? $this->input->post('pword') : NULL
        );

        // Check the username and password combination
        $result = $this->account_model->verify_details($params);

        if($result->num_rows() > 0)
        {
            $row = $result->row_array();

            $user['id'] = $row['id'];
            $user['account_type'] = $row['account_type'];

            // Check if unit is registered
            if($this->session->userdata('unit_name'))
            {
                $type = $this->session->userdata('unit_type');
                if(strcmp($type, 'front_desk') == 0 || strcmp($type, 'display') == 0)
                {
                    // Only managers can log in on front desks or displays
                    if(strcmp($row['account_type'], 'agent') == 0)
                    {
                        $this->session->set_flashdata('error', $this->lang->line('login_access'));
                        redirect(base_url() . 'login/');
                    }
                }

                $this->session->set_userdata($user);

                // Initialize statistics and Login DTR
                $this->statistics_model->initialize_account_statistics();
                $this->account_model->check_in($row['id']);

                if(strcmp($this->session->userdata('account_type'), 'agent') == 0)
                {
                    redirect(base_url() . 'agent/');
                }
                if(strcmp($this->session->userdata('account_type'), 'manager') == 0)
                {
                    redirect(base_url() . 'manager/');
                }
            }
            else
            {
                // Only managers can log in if the unit is unregistered
                if(strcmp($row['account_type'], 'agent') == 0)
                {
                    $this->session->set_flashdata('error', $this->lang->line('login_unit_unregister'));
                    redirect(base_url() . 'login/');
                }
                $this->session->set_userdata($user);
                redirect(base_url() . 'manager/');
            }
        }
        else
        {
            $this->session->set_flashdata('error', $this->lang->line('error_notice'));
            redirect(base_url() . 'login/');
        }
    }

}

/* End of file login.php */
/* Location: ./system/application/controllers/login.php */